﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.EmployeeService;
using MobileSales.Model.Employees;
using MobileSales.Website.Infrastructure.Messaging;
using MobileSales.Model.Exceptions;
using System.ServiceModel;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class RolesController : ControllerBase
    {
        //
        // GET: /Roles/

        #region Index
        public ActionResult Index()
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Role[] roles = employeeService.GetRoleList();
            employeeService.Close();
            if (roles == null)
                this.SetMessage(ErrorStrings.ErrorRoleList, MessageType.Error);
            return View(roles);
        } 
        #endregion

        #region Create
        //
        // GET: /Roles/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Roles/Create

        [HttpPost]
        public ActionResult Create(Role role)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.StoreRole(role);
                employeeService.Close();
                this.SetMessage(InfoStrings.RoleCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorRoleCreating, MessageType.Error);
            }
            return View(role);
        } 
        #endregion

        #region Edit
        //
        // GET: /Roles/Edit/5

        public ActionResult Edit(int id)
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Role role = employeeService.GetRole(id);
            employeeService.Close();
            if (role == null)
                this.SetMessage(ErrorStrings.ErrorRole, MessageType.Error);
            return View(role);
        }

        //
        // POST: /Roles/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Role role)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                role.Id = id;
                employeeService.StoreRole(role);
                employeeService.Close();
                this.SetMessage(InfoStrings.RoleUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorRoleUpdating, MessageType.Error);
            }
            return View(role);
        } 
        #endregion

        #region Delete
        //
        // GET: /Roles/Delete/5

        public ActionResult Delete(int id)
        {
            return Edit(id);
        }

        //
        // POST: /Roles/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.DeleteRole(id);
                employeeService.Close();
                this.SetMessage(InfoStrings.RoleDeleted, MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorRoleDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
